package my_interface

import (
	"sync"
	"time"
)

func ChannelT() {
	testChan := make(chan struct{})

	go func() {
		time.Sleep(time.Second)
		println("goroutine done.")
		close(testChan)
	}()

	println("main...")
	<-testChan // 阻塞，通道关闭立即解除阻塞
	println("main exit.")
}

func WaitGroupTest() {
	var wg sync.WaitGroup
	for i := 0; i < 10; i++ {
		wg.Add(1)
		go func(id int) {
			defer wg.Done()
			time.Sleep(time.Second)
			println("goroutine", id, "done")
		}(i)
	}

	println("main...")
	wg.Wait()
	println("main exit.")
}

func WaitGroupAdd() {
	var wg sync.WaitGroup
	wg.Add(1)
	go func() {
		wg.Wait()
		println("wait exit.")
	}()

	go func() {
		time.Sleep(time.Second)
		println("done.")
		wg.Done()
	}()

	wg.Wait()
	println("main exit")
}
